/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * 设计一个具有1位倒计时显示的十字路口交通灯系统。东西向直行7秒，左转5秒，黄灯3秒；南北向直行6
  * 秒，左转5秒，黄灯3秒，数码管显示北向红、黄、绿灯的时间。
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *__HAL_AFIO_REMAP_SWJ_DISABLE();
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define	ON	0
#define	OFF	1
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
uint8_t table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,
		0xc6,0xa1,0x86,0x8e,0xbf,0xbf,0xbf,0xbf,0xbf};
uint8_t timeCounter = 0;	//对定时0.5S到的次数进行计数的变量
uint8_t seconds = 0;		//秒数倒计时变量
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
//  SegA_GPIO_Port->BSRR = 0x000000c0;
  __HAL_TIM_CLEAR_FLAG(&htim3, TIM_SR_UIF);
  HAL_TIM_Base_Start_IT(&htim3);	//以中断方式起动定时器

//  HAL_TIM_Base_Start_IT(&htim4);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  switch(timeCounter)
	  {
	  	  case 0:
	  		  //南北红灯
	  		  HAL_GPIO_WritePin(SN_RED_GPIO_Port, SN_RED_Pin, ON);
	  		  HAL_GPIO_WritePin(SN_RED_GPIO_Port, SN_YELLOW_Pin |
	  				   SN_GREEN_F_Pin | SN_GREEN_L_Pin, OFF);
	  		  seconds = 18;
	  		  //东西直行绿灯7秒
	  		  HAL_GPIO_WritePin(EW_GREEN_F_GPIO_Port, EW_GREEN_F_Pin, ON);
	  		  HAL_GPIO_WritePin(EW_RED_GPIO_Port, EW_RED_Pin | EW_YELLOW_Pin |
	  				  EW_GREEN_L_Pin, OFF);
	  		  break;
	  	  case 14:
	  		  //东西直行绿灯关闭
	  		  HAL_GPIO_WritePin(EW_GREEN_F_GPIO_Port, EW_GREEN_F_Pin, OFF);
	  		  //东西黄灯亮
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, ON);
	  		  break;
	  	  case 15:
	  		  //东西黄灯灭
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, OFF);break;
	  	  case 16:
	  		  //东西黄灯亮
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, ON);break;
	  	  case 17:
	  		  //东西黄灯灭
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, OFF);break;
	  	  case 18:
	  		  //东西黄灯亮
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, ON);break;
	  	  case 19:
	  		  //东西黄灯灭
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, OFF);break;
	  	  case 20:
	  		  //南北红灯
	  		  HAL_GPIO_WritePin(SN_RED_GPIO_Port, SN_RED_Pin, ON);
	  		  HAL_GPIO_WritePin(SN_RED_GPIO_Port, SN_YELLOW_Pin |
	  				   SN_GREEN_F_Pin | SN_GREEN_L_Pin, OFF);
	  		  //东西左转绿灯5秒
	  		  HAL_GPIO_WritePin(EW_GREEN_L_GPIO_Port, EW_GREEN_L_Pin, ON);
	  		  HAL_GPIO_WritePin(EW_RED_GPIO_Port, EW_RED_Pin | EW_YELLOW_Pin |
	  				  EW_GREEN_F_Pin, OFF);
	  		  break;
	  	  case 30:
	  		  //东西左转绿灯灭
	  		  HAL_GPIO_WritePin(EW_GREEN_L_GPIO_Port, EW_GREEN_L_Pin, OFF);
	  		  //东西黄灯亮
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, ON);
	  		  break;
	  	  case 31:
	  		  //东西黄灯灭
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, OFF);break;
	  	  case 32:
	  		  //东西黄灯亮
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, ON);break;
	  	  case 33:
	  		  //东西黄灯灭
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, OFF);break;
	  	  case 34:
	  		  //东西黄灯亮
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, ON);break;
	  	  case 35:
	  		  //东西黄灯灭
	  		  HAL_GPIO_WritePin(EW_YELLOW_GPIO_Port, EW_YELLOW_Pin, OFF);break;
	  	  case 36:
	  		  //东西红灯
	  		  HAL_GPIO_WritePin(EW_RED_GPIO_Port, EW_RED_Pin, ON);
	  		  HAL_GPIO_WritePin(EW_RED_GPIO_Port, EW_YELLOW_Pin |
	  				   EW_GREEN_F_Pin | EW_GREEN_L_Pin, OFF);
	  		  //南北直行绿灯6�???????
	  		  HAL_GPIO_WritePin(SN_GREEN_F_GPIO_Port, SN_GREEN_F_Pin, ON);
	  		  HAL_GPIO_WritePin(SN_RED_GPIO_Port, SN_RED_Pin | SN_YELLOW_Pin |
	  				  SN_GREEN_L_Pin, OFF);
	  		  seconds = 6;
	  		  break;
	  	  case 48:
	  		  //南北直行绿灯�???????
	  		  HAL_GPIO_WritePin(SN_GREEN_F_GPIO_Port, SN_GREEN_F_Pin, OFF);
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, ON);
	  		  seconds = 3;
	  		  break;
	  	  case 49:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, OFF);break;
	  	  case 50:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, ON);break;
	  	  case 51:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, OFF);break;
	  	  case 52:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, ON);break;
	  	  case 53:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, OFF);break;
	  	  case 54:
	  		  //东西红灯
	  		  HAL_GPIO_WritePin(EW_RED_GPIO_Port, EW_RED_Pin, ON);
	  		  HAL_GPIO_WritePin(EW_RED_GPIO_Port, EW_YELLOW_Pin |
	  				   EW_GREEN_F_Pin | EW_GREEN_L_Pin, OFF);
	  		  //南北左转绿灯5�???????
	  		  HAL_GPIO_WritePin(SN_GREEN_L_GPIO_Port, SN_GREEN_L_Pin, ON);
	  		  HAL_GPIO_WritePin(SN_RED_GPIO_Port, SN_RED_Pin | SN_YELLOW_Pin |
	  				  SN_GREEN_F_Pin, OFF);
	  		  seconds = 5;
	  		  break;
	  	  case 64:
	  		  //南北左转绿灯�???????
	  		  HAL_GPIO_WritePin(SN_GREEN_L_GPIO_Port, SN_GREEN_L_Pin, OFF);
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, ON);
	  		  seconds = 3;
	  		  break;
	  	  case 65:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, OFF);break;
	  	  case 66:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, ON);break;
	  	  case 67:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, OFF);break;
	  	  case 68:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, ON);break;
	  	  case 69:
	  		  //南北黄灯�???????
	  		  HAL_GPIO_WritePin(SN_YELLOW_GPIO_Port, SN_YELLOW_Pin, OFF);break;
	  	  default:break;
	  }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(htim);
  if(htim == &htim3)
  {
	  if(timeCounter > 69)
		  timeCounter = 0;
	  else
		  timeCounter++;
	  if(seconds > 0)
		  seconds--;
	  SegA_GPIO_Port->ODR = (uint32_t)table[seconds];
  }
  __HAL_TIM_CLEAR_FLAG(&htim3, TIM_SR_UIF);
  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
   */
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
